import 'dart:convert';

import 'package:flutter_app/utils/sp_utils.dart';
import 'package:get/get.dart';
import 'package:image_pickers/image_pickers.dart';

/**
 * @Description: 文件信息 json保存
 * @Author: 下雨了
 * @CreateDate: 2022-08-01 10:15
 * @UpdateUser: 更新者
 * @UpdateDate:  2022-08-01 10:15
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
///

/// 保存sp 的常量
const String FILE_SAVE_JSON = "fileSaveJson";

class ImageFileInfoUtils {
  // 保存文件信息到 sp文件 json
  Future<bool> saveFileInfoToJson(List<ImageFileInfo> info) async {
    String json = jsonEncode(info);
    return sp.setString(FILE_SAVE_JSON, json);
  }

  // 获取文件信息
  Future<List<ImageFileInfo>> getFileInfo() async {
    List<ImageFileInfo> imageFileInfoList = [];
    String? s = await sp.getString(FILE_SAVE_JSON);
    if (s != null) {
      List<dynamic> list = jsonDecode(s);
      imageFileInfoList = list.map((e) => ImageFileInfo.fromJson(e)).toList();
    }
    return imageFileInfoList;
  }

  // 根据小组id 查询 同一组的数据
  Future<List<ImageFileInfo>> getFileInfoInGroupId(String groupId) async {
    List<ImageFileInfo> imageFileInfoList = await getFileInfo();

    return imageFileInfoList.where((element) => element.groupId == groupId).toList();
  }

  // 根据小组id 删除 同一组数据
  Future<bool> deleteInGroupId(String groupId) async {
    List<ImageFileInfo> imageFileInfoList = await getFileInfo();
    imageFileInfoList = imageFileInfoList.where((element) => element.groupId == groupId).toList();

    for (ImageFileInfo info in imageFileInfoList) {
      await delete(info);
    }
    return true;
  }

  // 添加 单条 多条 ImageFileInfo
  Future<bool> addItem(List<ImageFileInfo> info) async {
    List<ImageFileInfo> imageFileInfoList = await getFileInfo();
    imageFileInfoList.addAll(info);
    return saveFileInfoToJson(imageFileInfoList);
  }

  // 删除 单条 多条 ImageFileInfo
  Future<bool> delete(ImageFileInfo info) async {
    List<ImageFileInfo> imageFileInfoList = await getFileInfo();
    imageFileInfoList.removeWhere((element) => info.name == element.name);
    return saveFileInfoToJson(imageFileInfoList);
  }

  // 删除 全部
  Future<bool> removeAll() async {
    return await sp.remove(FILE_SAVE_JSON);
  }

  // List<ImageFileInfo> 转换成 List<Media>
  List<Media> listImageFileInfoToListMedia(List<ImageFileInfo> imageFileInfo) {
    List<Media> list = [];
    imageFileInfo.forEach((element) {
      Media media = new Media();
      media.path = element.path;
      media.thumbPath = element.thumbnail;
      if (element.type == "image") {
        media.galleryMode = GalleryMode.image;
      } else {
        media.galleryMode = GalleryMode.video;
      }
      list.add(media);
    });
    return list;
  }

  Media imageFileInfoToMedia(ImageFileInfo imageFileInfo) {
    Media media = new Media();
    media.path = imageFileInfo.path;
    media.thumbPath = imageFileInfo.thumbnail;
    if (imageFileInfo.type == "image") {
      media.galleryMode = GalleryMode.image;
    } else {
      media.galleryMode = GalleryMode.video;
    }
    return media;
  }

  // List<Media> 转换成 List<ImageFileInfo>
  List<ImageFileInfo> listMediaToListImageFileInfo(List<Media> medias) {
    List<ImageFileInfo> list = [];
    medias.forEach((element) {
      ImageFileInfo info = new ImageFileInfo();
      info.name = element.name;
      info.thumbnail = element.thumbPath;
      info.path = element.path;
      info.dateTime = element.dateTime;
      info.longitude = element.longitude.toString();
      info.latitude = element.latitude.toString();
      info.address = element.address;
      info.type = element.galleryMode == GalleryMode.image ? "image" : "video";

      list.add(info);
    });
    return list;
  }
}

/// 要保存文件的 bean
class ImageFileInfo {
  String? id; //
  String? groupId; // 小组id
  String? photographer; // 拍摄人
  String? name; //名称
  String? path; //文件地址

  String? type; //类型
  String? thumbnail; //缩略图地址
  String? duration; //视频时间
  String? time; //时间段   am pm
  String? dateTime; // 拍摄日期
  String? longitude; //经度
  String? latitude; //维度
  String? address; //详细地址

  RxBool isChose = false.obs;

  ImageFileInfo({
    this.id,
    this.groupId,
    this.photographer,
    this.name,
    this.type,
    this.path,
    this.thumbnail,
    this.duration,
    this.time,
    this.dateTime,
    this.longitude,
    this.latitude,
    this.address,
  });

  ImageFileInfo.fromJson(dynamic json) {
    id = json['id'];
    groupId = json['groupId'];
    photographer = json['photographer'];
    name = json['name'];
    path = json['path'];
    type = json['type'];
    thumbnail = json['thumbnail'];
    duration = json['duration'];
    time = json['time'];
    dateTime = json['dateTime'];
    longitude = json['longitude'];
    latitude = json['latitude'];
    address = json['address'];
  }

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['id'] = id;
    map['groupId'] = groupId;
    map['photographer'] = photographer;
    map['name'] = name;
    map['path'] = path;
    map['type'] = type;
    map['thumbnail'] = thumbnail;
    map['duration'] = duration;
    map['time'] = time;
    map['dateTime'] = dateTime;
    map['longitude'] = longitude;
    map['latitude'] = latitude;
    map['address'] = address;
    return map;
  }
}
